package com.mycjj.android.activity;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.R;
import com.mycjj.android.adapter.TAdapter;
import com.mycjj.android.bean.MessageBaseBean;
import com.mycjj.android.bean.MessageBaseBean.MessageBean.MsgInfo;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.mycjj.android.util.Preferences;

//消息提醒列表
public class My_MessageListActivity extends BaseActivity{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_me_message_list);
		initHeader();
		init();
		initData();
	}
	private ImageView mHeaderLeftImageview;
	private Button mHeaderRightButton;
	private TextView mHeaderTitleTextView;
	private void initHeader(){
		mHeaderLeftImageview = (ImageView) findViewById(R.id.header_left_button);
		mHeaderLeftImageview.setImageResource(R.drawable.back);
		mHeaderRightButton = (Button) findViewById(R.id.header_right_button);
		mHeaderRightButton.setVisibility(View.GONE);
		mHeaderTitleTextView = (TextView) findViewById(R.id.header_title_textview);
		mHeaderTitleTextView.setText("消息");
		mHeaderLeftImageview.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				My_MessageListActivity.this.finish();
			}
		});
	}
	
	private ListView listview;
	private void init(){
		listview = (ListView)findViewById(R.id.listview);
		adapter = new GameConfigAdapter(this);
		listview.setAdapter(adapter);
		
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
					Intent intent = new Intent(My_MessageListActivity.this,My_MessageDetailActivity.class);
					intent.putExtra("id", String.valueOf(dataList.get(position).getMessage_id()));
					startActivity(intent);
			}
			
		});
	}
	List<MsgInfo> dataList;
	private void initData(){
		request = new ProjectHttpRequestImpl(this, true);
		RequestParams params = new RequestParams();
		params.put("client_id",ProjectHttpRequestInterface.client_id_key);
		params.put("token", Preferences.getSharedPreferences(this, "user_token", ""));
		params.put("offset", "0");
		params.put("num", "100");
		request.requestMsgGetsDoneHandler("", params, new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(String content) {
				MessageBaseBean baseBean = dataManager.getMessageBaseBean();
				if(baseBean != null){
					if(baseBean.getCode() == 200){
						dataList = baseBean.getData().getList();
						adapter.setList(dataList);
						dataManager.setNo_read_count(0);
					}else{
						Toast.makeText(My_MessageListActivity.this, baseBean.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}else{
					
				}
			}
		});
	}
	
	private GameConfigAdapter adapter;
	public class GameConfigAdapter extends TAdapter<MsgInfo>{

		public GameConfigAdapter(Context mContext) {
			super(mContext);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(R.layout.item_my_message_view, null);
				holder = new ViewHolder();
				holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
				holder.tv_desc = (TextView) convertView.findViewById(R.id.tv_desc);
				holder.tv_date= (TextView) convertView.findViewById(R.id.tv_date);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			MsgInfo bean = mList.get(position);
			holder.tv_name.setText(bean.getMessage_from_member());
			holder.tv_desc.setText(bean.getMessage_content());
			holder.tv_date.setText(bean.getMessage_time());
			return convertView;
		}
		
		
		class ViewHolder{
			TextView tv_name;
			TextView tv_desc;
			TextView tv_date;
		}
		
		

	}
}
